﻿Imports System.Data.SqlClient
Imports System.IO

Public Class frmVisorAdjuntos
    Sub CargaArchivos()
        dgAdjuntos.Rows.Clear()

        Dim cnconn As New SqlConnection
        Dim strSql As String
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        Try
            cnconn.ConnectionString = CitraConnection
            strSql = "SELECT * FROM Cot_Anexos WHERE IdGeneral=" & IdGeneral & " ORDER BY IdAnexo"

            cmdBuscar.Connection = cnconn
            cmdBuscar.CommandText = strSql

            cnconn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Dim Tipo As String
            Dim TipoUcase As String = ""



            Do While rdBuscar.Read()
                Tipo = UCase(rdBuscar("Extencion"))
                Select Case Tipo
                    Case "PDF"
                        TipoUcase = "PDF"
                    Case "XLSX"
                        TipoUcase = "Excel 2007-2013"
                    Case "XLS"
                        TipoUcase = "Excel 1997-2003"
                    Case "DOC"
                        TipoUcase = "Word 2007-2013"
                    Case "DOCX"
                        TipoUcase = "Word 1997-2003"
                End Select
                dgAdjuntos.Rows.Add(rdBuscar("IdAnexo"), rdBuscar("Descripcion"), TipoUcase)
            Loop


        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")
        Finally
            If cnconn.State = ConnectionState.Open Then
                cnconn.Close()
            End If
        End Try
    End Sub
    Private Sub AbrirArchivoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AbrirArchivoToolStripMenuItem.Click
        Dim cnconn As New SqlConnection
        Dim strSql As String
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        
        Try
            cnconn.ConnectionString = CitraConnection
            strSql = "SELECT * FROM Cot_Anexos WHERE idAnexo=" & dgAdjuntos.CurrentRow.Cells(0).Value & " ORDER BY IdAnexo"

            cmdBuscar.Connection = cnconn
            cmdBuscar.CommandText = strSql

            cnconn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Dim I As Integer = 0
            Dim myext As String = ""
            Dim Archivo As String = ""
            Dim Folio As Integer = 0
            Dim IdDocumento_ As Integer = 0
            Dim fs As FileStream
            Dim bw As System.IO.BinaryWriter


            Do While rdBuscar.Read()
                Dim filedata(rdBuscar.GetBytes(2, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte
                rdBuscar.GetBytes(2, 0, filedata, 0, filedata.Length)
                myext = rdBuscar("Extencion")
                IdDocumento_ = BuscarIntDatoCitra("SELECT IdDocumento FROM Cot_Generales WHERE IdGeneral=" & rdBuscar("IdGeneral"))
                Folio = BuscarIntDatoCitra("SELECT Folio FROM Cot_Documentos WHERE IdDocumento=" & IdDocumento_)
                Archivo = "c:\Cotizacion # " & Folio & " " & rdBuscar("Descripcion") & "." & myext

                Try
                    My.Computer.FileSystem.DeleteFile(Archivo, FileIO.UIOption.AllDialogs, FileIO.RecycleOption.SendToRecycleBin, FileIO.UICancelOption.DoNothing)
                Catch ex As Exception

                End Try

                fs = New FileStream(Archivo, FileMode.OpenOrCreate, FileAccess.Write)
                bw = New System.IO.BinaryWriter(fs)
                bw.Write(filedata)
                bw.Flush()
                bw.Close()
                fs.Close()
                I = I + 1

            Loop

            Dim Command As New Process
            Command.StartInfo.FileName = Archivo
            Command.StartInfo.UseShellExecute = True
            Command.StartInfo.CreateNoWindow = True
            Command.Start()


        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")
        Finally
            If cnconn.State = ConnectionState.Open Then
                cnconn.Close()
            End If
        End Try
    End Sub

    Private Sub frmVisorAdjuntos_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        CargaArchivos()

    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Close()

    End Sub
End Class